using System;
using System.Data;
using System.Configuration;
using CodeWork.Library.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Data.SqlTypes;
using CodeWork.Library;
using System.Text;
using System.Data.SqlClient;

/// <summary>
/// ORM object that maps to tbl_DiscountCoupon
/// </summary>
namespace EasyCommerce.DAL
{
    public class DiscountCoupon : SqlServerDAO
    {
        /// <summary>
        /// Self table parameters
        /// </summary>
        private int _id;
		private string _couponName;
		private string _couponCode;
		private double _couponDiscountPrice;
		private string _couponImageUrl;
		private string _couponDescription;
		private bool _isActive;
		private DateTime _createDate;
		private DateTime _expiryDate;

        /// <summary>
        /// Foriegn key parameters
        /// </summary>
        private List<DiscountCouponAndSalesOrder> _usedDiscountCouponAndSalesOrder;
		private bool _isUsedDiscountCouponAndSalesOrderLoaded;
		

        /// <summary>
        /// Self table properties
        /// </summary>
		public int Id
		{
			get { return this._id; }
			set { this._id = value; }
		}

		public string CouponName
		{
			get { return this._couponName; }
			set { this._couponName = value; }
		}

		public string CouponCode
		{
			get { return this._couponCode; }
			set { this._couponCode = value; }
		}

		public double CouponDiscountPrice
		{
			get { return this._couponDiscountPrice; }
			set { this._couponDiscountPrice = value; }
		}

		public string CouponImageUrl
		{
			get { return this._couponImageUrl; }
			set { this._couponImageUrl = value; }
		}

		public string CouponDescription
		{
			get { return this._couponDescription; }
			set { this._couponDescription = value; }
		}

		public bool IsActive
		{
			get { return this._isActive; }
			set { this._isActive = value; }
		}

		public DateTime CreateDate
		{
			get { return this._createDate; }
			set { this._createDate = value; }
		}

		public DateTime ExpiryDate
		{
			get { return this._expiryDate; }
			set { this._expiryDate = value; }
		}
		
        /// <summary>
        /// Foreign key properties, loaded on demand only
        /// </summary>
        public List<DiscountCouponAndSalesOrder> UsedDiscountCouponAndSalesOrder
		{
			get
			{
				if (!this._isUsedDiscountCouponAndSalesOrderLoaded)
				{
					List<Condition> conditions = new List<Condition>();
					conditions.Add(new Condition("used_discountcoupon_id", ConditionOperator.Equal,this._id, JoinOperator.FirstValue));
					DiscountCouponAndSalesOrder discountCouponAndSalesOrder = new DiscountCouponAndSalesOrder();
					this._usedDiscountCouponAndSalesOrder = discountCouponAndSalesOrder.Load(conditions);
					this._isUsedDiscountCouponAndSalesOrderLoaded = true;
				}
				return this._usedDiscountCouponAndSalesOrder;
			}
			set
			{
				this._usedDiscountCouponAndSalesOrder = value;
				this._isUsedDiscountCouponAndSalesOrderLoaded = true;
			}
		}
		

        /// <summary>
        /// Empty Constructor
        /// </summary>
        public DiscountCoupon() { }

        /// <summary>
        /// Parameterized constructor
        /// </summary>
        /// <param name="id"></param>
		/// <param name="couponName"></param>
		/// <param name="couponCode"></param>
		/// <param name="couponDiscountPrice"></param>
		/// <param name="couponImageUrl"></param>
		/// <param name="couponDescription"></param>
		/// <param name="isActive"></param>
		/// <param name="createDate"></param>
		/// <param name="expiryDate"></param>
        public DiscountCoupon
        (
            int id,
			string couponName,
			string couponCode,
			double couponDiscountPrice,
			string couponImageUrl,
			string couponDescription,
			bool isActive,
			DateTime createDate,
			DateTime expiryDate
        )
        {
            this._id = id;
			this._couponName = couponName;
			this._couponCode = couponCode;
			this._couponDiscountPrice = couponDiscountPrice;
			this._couponImageUrl = couponImageUrl;
			this._couponDescription = couponDescription;
			this._isActive = isActive;
			this._createDate = createDate;
			this._expiryDate = expiryDate;
        }

        /// <summary>
        /// Static save method. Saves the current instance in the table using the provided values using the
        /// primary key id. Expects that the Id is a primary key of an existing record in table.
        /// </summary>
        /// <param name="id"></param>
		/// <param name="couponName"></param>
		/// <param name="couponCode"></param>
		/// <param name="couponDiscountPrice"></param>
		/// <param name="couponImageUrl"></param>
		/// <param name="couponDescription"></param>
		/// <param name="isActive"></param>
		/// <param name="createDate"></param>
		/// <param name="expiryDate"></param>
        /// <returns>Boolean result as success or failure</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool Save
        (
            int id,
			string couponName,
			string couponCode,
			double couponDiscountPrice,
			string couponImageUrl,
			string couponDescription,
			bool isActive,
			DateTime createDate,
			DateTime expiryDate
        )
        {
            DiscountCoupon discountCoupon = new DiscountCoupon();
            discountCoupon.Id = id;
			discountCoupon.CouponName = couponName;
			discountCoupon.CouponCode = couponCode;
			discountCoupon.CouponDiscountPrice = couponDiscountPrice;
			discountCoupon.CouponImageUrl = couponImageUrl;
			discountCoupon.CouponDescription = couponDescription;
			discountCoupon.IsActive = isActive;
			discountCoupon.CreateDate = createDate;
			discountCoupon.ExpiryDate = expiryDate;

            return discountCoupon.Save();
        }

        /// <summary>
        /// Saves the current instance and returns result
        /// </summary>
        /// <returns>Boolean result as success or failure</returns>
        public override bool Save()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, Id);
			AddCommandParameter(command, "@CouponName", SqlDbType.NVarChar, 250, ParameterDirection.Input, CouponName);
			AddCommandParameter(command, "@CouponCode", SqlDbType.NVarChar, 50, ParameterDirection.Input, CouponCode);
			AddCommandParameter(command, "@CouponDiscountPrice", SqlDbType.Decimal, 0, ParameterDirection.Input, CouponDiscountPrice);
			AddCommandParameter(command, "@CouponImageUrl", SqlDbType.NVarChar, 500, ParameterDirection.Input, CouponImageUrl);
			AddCommandParameter(command, "@CouponDescription", SqlDbType.NVarChar, -1, ParameterDirection.Input, CouponDescription);
			AddCommandParameter(command, "@IsActive", SqlDbType.Bit, 0, ParameterDirection.Input, IsActive);
			AddCommandParameter(command, "@CreateDate", SqlDbType.DateTime, 0, ParameterDirection.Input, CreateDate);
			AddCommandParameter(command, "@ExpiryDate", SqlDbType.DateTime, 0, ParameterDirection.Input, ExpiryDate);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_DiscountCoupon_Save.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// Saves the current instance based on provided conditions. This method assumes that
        /// conditions are provided in correct order and in proper format.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>Boolean result as success or failure</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public bool Save(List<Condition> conditions)
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            AddCommandParameter(command, "@NewCouponName", SqlDbType.NVarChar, 250, ParameterDirection.Input, CouponName);
			AddCommandParameter(command, "@NewCouponCode", SqlDbType.NVarChar, 50, ParameterDirection.Input, CouponCode);
			AddCommandParameter(command, "@NewCouponDiscountPrice", SqlDbType.Decimal, 0, ParameterDirection.Input, CouponDiscountPrice);
			AddCommandParameter(command, "@NewCouponImageUrl", SqlDbType.NVarChar, 500, ParameterDirection.Input, CouponImageUrl);
			AddCommandParameter(command, "@NewCouponDescription", SqlDbType.NVarChar, -1, ParameterDirection.Input, CouponDescription);
			AddCommandParameter(command, "@NewIsActive", SqlDbType.Bit, 0, ParameterDirection.Input, IsActive);
			AddCommandParameter(command, "@NewCreateDate", SqlDbType.DateTime, 0, ParameterDirection.Input, CreateDate);
			AddCommandParameter(command, "@NewExpiryDate", SqlDbType.DateTime, 0, ParameterDirection.Input, ExpiryDate);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_DiscountCoupon_SaveConditional.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// Static creational method. This method actually adds a new entry in the table with
        /// the provided information.
        /// </summary>
        /// <param name="couponName"></param>
		/// <param name="couponCode"></param>
		/// <param name="couponDiscountPrice"></param>
		/// <param name="couponImageUrl"></param>
		/// <param name="couponDescription"></param>
		/// <param name="isActive"></param>
		/// <param name="createDate"></param>
		/// <param name="expiryDate"></param>
        /// <returns>Instance of the created object</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static DiscountCoupon Create
        (
            string couponName,
			string couponCode,
			double couponDiscountPrice,
			string couponImageUrl,
			string couponDescription,
			bool isActive,
			DateTime createDate,
			DateTime expiryDate
        )
        {
            DiscountCoupon discountCoupon = new DiscountCoupon();
            discountCoupon.CouponName = couponName;
			discountCoupon.CouponCode = couponCode;
			discountCoupon.CouponDiscountPrice = couponDiscountPrice;
			discountCoupon.CouponImageUrl = couponImageUrl;
			discountCoupon.CouponDescription = couponDescription;
			discountCoupon.IsActive = isActive;
			discountCoupon.CreateDate = createDate;
			discountCoupon.ExpiryDate = expiryDate;

            return (discountCoupon.Create() ? discountCoupon : null);
        }

        /// <summary>
        /// Instance creational method. Inserts the current object as a new entry in table.
        /// </summary>
        /// <returns>Boolean result as success or failure</returns>
        public override bool Create()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@CouponName", SqlDbType.NVarChar, 250, ParameterDirection.Input, CouponName);
			AddCommandParameter(command, "@CouponCode", SqlDbType.NVarChar, 50, ParameterDirection.Input, CouponCode);
			AddCommandParameter(command, "@CouponDiscountPrice", SqlDbType.Decimal, 0, ParameterDirection.Input, CouponDiscountPrice);
			AddCommandParameter(command, "@CouponImageUrl", SqlDbType.NVarChar, 500, ParameterDirection.Input, CouponImageUrl);
			AddCommandParameter(command, "@CouponDescription", SqlDbType.NVarChar, -1, ParameterDirection.Input, CouponDescription);
			AddCommandParameter(command, "@IsActive", SqlDbType.Bit, 0, ParameterDirection.Input, IsActive);
			AddCommandParameter(command, "@CreateDate", SqlDbType.DateTime, 0, ParameterDirection.Input, CreateDate);
			AddCommandParameter(command, "@ExpiryDate", SqlDbType.DateTime, 0, ParameterDirection.Input, ExpiryDate);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_DiscountCoupon_Create.ToString());

            Id = (int)GetOneData(command);
            return (Id == 0 ? false : true);
        }

        /// <summary>
        /// Static deletion method. Deletes a record from table using the provided primary key id.
        /// </summary>
        /// <param name="id">Primary key</param>
        /// <returns>Boolean result as success or failure</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public static bool Delete(int id)
        {
            DiscountCoupon discountCoupon = new DiscountCoupon();
            discountCoupon.Id = id;

            return discountCoupon.Delete();
        }

        /// <summary>
        /// Static deletion method. Deletes a list record from table using the provided primary keys id.
        /// </summary>
        /// <param name="ids">List of primarky key id</param>
        /// <returns>Boolean result as success or failure</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public static bool Delete(List<int> ids)
        {
            string csvIds = Utility.MakeCSV(ids);

            DiscountCoupon discountCoupon = new DiscountCoupon();

            return discountCoupon.Delete(csvIds);
        }

        /// <summary>
        /// Static deletion method. Deletes records from table using provided conditions.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>Boolean result as success or failure</returns>
        public static bool Delete(List<Condition> conditions)
        {
            DiscountCoupon discountCoupon = new DiscountCoupon();

            return discountCoupon.ConditionalDelete(conditions);
        }

        /// <summary>
        /// This is helper method for conditional delete method above. Not for other general use.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>Boolean result as success or failure</returns>
        private bool ConditionalDelete(List<Condition> conditions)
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_DiscountCoupon_DeleteConditional.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// This is a helper method for multiple id delete method. Not for general use.
        /// </summary>
        /// <param name="csvIds">Comma seperated primary key values as string</param>
        /// <returns>Boolean result as success or failure</returns>
        private bool Delete(string csvIds)
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@Ids", SqlDbType.NVarChar, 500, ParameterDirection.Input, csvIds);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_DiscountCoupon_DeleteMultiple.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// Instance deletion method. Deletes the current instance from table.
        /// </summary>
        /// <returns>Boolean result as success or failure</returns>
        public override bool Delete()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, Id);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_DiscountCoupon_DeleteOne.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// Static selection method. Used for loading a perticular data using primary key id.
        /// </summary>
        /// <param name="id">Primary key</param>
        /// <returns>Instance of the loaded object</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static DiscountCoupon Load(int id)
        {
            DiscountCoupon discountCoupon = new DiscountCoupon();
            discountCoupon.Id = id;

            if (discountCoupon.Load())
                return discountCoupon;
            else
                return null;
        }

        /// <summary>
        /// Instance selection method that fetches data using paging and sorting and also with 
        /// custom conditions.
        /// </summary>
        /// <param name="oderBy">Sorting order</param>
        /// <param name="startRow">Page start position</param>
        /// <param name="pageSize">Number of maximum record to be shown</param>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>List of instances of loaded objects</returns>
        public List<DiscountCoupon> Load(string orderBy, int startRow, int pageSize, List<Condition> conditions)
        {
            List<DiscountCoupon> discountCoupons = new List<DiscountCoupon>();
			
			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@OrderBy", SqlDbType.NVarChar, 50, ParameterDirection.Input, orderBy);
            AddCommandParameter(command, "@StartRow", SqlDbType.Int, 0, ParameterDirection.Input, startRow);
            AddCommandParameter(command, "@PageSize", SqlDbType.Int, 0, ParameterDirection.Input, pageSize);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_DiscountCoupon_LoadConditionalPaged.ToString());

            List<BaseDAO> dataList = GetListData(command);

            foreach (BaseDAO data in dataList)
            {
                DiscountCoupon discountCoupon = (DiscountCoupon)data;
                discountCoupons.Add(discountCoupon);
            }

            return discountCoupons;
        }

        /// <summary>
        /// Counts the number of total record available
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>Count of the records</returns>
        public int LoadCount(List<Condition> conditions)
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_DiscountCoupon_LoadConditionalPagedCount.ToString());

            int returnValue = (int)GetOneData(command);
            return returnValue;
        }

        /// <summary>
        /// Instance selection method that fetches data using paging and sorting.
        /// </summary>
        /// <param name="oderBy">Sorting order</param>
        /// <param name="startRow">Page start position</param>
        /// <param name="pageSize">Number of maximum record to be shown</param>
        /// <returns>List of instances of loaded objects</returns>
        public List<DiscountCoupon> Load(string orderBy, int startRow, int pageSize)
        {
            List<DiscountCoupon> discountCoupons = new List<DiscountCoupon>();
            
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@OrderBy", SqlDbType.NVarChar, 50, ParameterDirection.Input, orderBy);
            AddCommandParameter(command, "@StartRow", SqlDbType.Int, 0, ParameterDirection.Input, startRow);
            AddCommandParameter(command, "@PageSize", SqlDbType.Int, 0, ParameterDirection.Input, pageSize);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_DiscountCoupon_LoadPaged.ToString());

            List<BaseDAO> dataList = GetListData(command);

            foreach (BaseDAO data in dataList)
            {
                DiscountCoupon discountCoupon = (DiscountCoupon)data;
                discountCoupons.Add(discountCoupon);
            }

            return discountCoupons;
        }

        /// <summary>
        /// Counts the number of total record available
        /// </summary>
        /// <returns>Count of the records</returns>
        public int LoadCount()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_DiscountCoupon_LoadPagedCount.ToString());

            int returnValue = (int)GetOneData(command);
            return returnValue;
        }

        /// <summary>
        /// Instance selection method. Used for reloading the current object from table. This method uses
        /// the Id of the current object to load and expects that the Id is correct.
        /// </summary>
        /// <returns>Boolean result as success or failure</returns>
        public override bool Load()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, Id);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_DiscountCoupon_Load.ToString());

            List<BaseDAO> dataList = GetListData(command);

            foreach (BaseDAO data in dataList)
            {
                DiscountCoupon discountCoupon = (DiscountCoupon)data;

                this._id = discountCoupon._id;
				this._couponName = discountCoupon._couponName;
				this._couponCode = discountCoupon._couponCode;
				this._couponDiscountPrice = discountCoupon._couponDiscountPrice;
				this._couponImageUrl = discountCoupon._couponImageUrl;
				this._couponDescription = discountCoupon._couponDescription;
				this._isActive = discountCoupon._isActive;
				this._createDate = discountCoupon._createDate;
				this._expiryDate = discountCoupon._expiryDate;

                break;
            }

            return (dataList.Count > 0 ? true : false);
        }

        /// <summary>
        /// Instance selection method. Used for loading multiple record based on provided conditions.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>List of instances of loaded objects</returns>
        public List<DiscountCoupon> Load(List<Condition> conditions)
        {
            List<DiscountCoupon> discountCoupons = new List<DiscountCoupon>();

			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_DiscountCoupon_LoadConditional.ToString());

            List<BaseDAO> dataList = GetListData(command);

            foreach (BaseDAO data in dataList)
            {
                DiscountCoupon discountCoupon = (DiscountCoupon)data;
                discountCoupons.Add(discountCoupon);
            }

            return discountCoupons;
        }

        /// <summary>
        /// Prepares the conditional parameters for use with Create/ Save/ Load/ Delete methods
        /// that use custom condition. Prepares the user Command object for using conditions.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        private void PrepareConditionalParameters(SqlCommand command, List<Condition> conditions)
        {
            StringBuilder condition = new StringBuilder();

            object id = null;
			object couponName = null;
			object couponCode = null;
			object couponDiscountPrice = null;
			object couponImageUrl = null;
			object couponDescription = null;
			object isActive = null;
			object createDate = null;
			object expiryDate = null;

            for (int i = 0; i < conditions.Count; i++)
            {
                string fieldName = Utility.ToHungarianNotation(conditions[i].FieldName);

                if (conditions[i].JoiningCondition == JoinOperator.FirstValue)
                {
                    condition.Append("WHERE ");
                }
                else if (conditions[i].JoiningCondition == JoinOperator.And)
                {
                    condition.Append(" AND ");
                }
                else if (conditions[i].JoiningCondition == JoinOperator.Or)
                {
                    condition.Append(" OR ");
                }

                condition.Append(conditions[i].FieldName).Append(" ").Append(Condition.MapCondition(conditions[i].ConditionalOperator)).Append("@" + fieldName);

                if ((conditions[i].ConditionalOperator == ConditionOperator.Like ||
                    conditions[i].ConditionalOperator == ConditionOperator.NotLike))
                {
                    if (string.IsNullOrEmpty(conditions[i].Value.ToString()))
                        conditions[i].Value = "%%%";
                    else
                        conditions[i].Value = "%" + conditions[i].Value.ToString() + "%";
                }

                if (fieldName == TableColumns.Id.ToString())
                    id = conditions[i].Value;
                else if (fieldName == TableColumns.CouponName.ToString())
					couponName = conditions[i].Value;
				else if (fieldName == TableColumns.CouponCode.ToString())
					couponCode = conditions[i].Value;
				else if (fieldName == TableColumns.CouponDiscountPrice.ToString())
					couponDiscountPrice = conditions[i].Value;
				else if (fieldName == TableColumns.CouponImageUrl.ToString())
					couponImageUrl = conditions[i].Value;
				else if (fieldName == TableColumns.CouponDescription.ToString())
					couponDescription = conditions[i].Value;
				else if (fieldName == TableColumns.IsActive.ToString())
					isActive = conditions[i].Value;
				else if (fieldName == TableColumns.CreateDate.ToString())
					createDate = conditions[i].Value;
				else if (fieldName == TableColumns.ExpiryDate.ToString())
					expiryDate = conditions[i].Value;
            }

            AddCommandParameter(command, "@Condition", SqlDbType.NVarChar, 2000, ParameterDirection.Input, condition.ToString());
            AddCommandParameter(command, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
			AddCommandParameter(command, "@CouponName", SqlDbType.NVarChar, 250, ParameterDirection.Input, couponName);
			AddCommandParameter(command, "@CouponCode", SqlDbType.NVarChar, 50, ParameterDirection.Input, couponCode);
			AddCommandParameter(command, "@CouponDiscountPrice", SqlDbType.Decimal, 0, ParameterDirection.Input, couponDiscountPrice);
			AddCommandParameter(command, "@CouponImageUrl", SqlDbType.NVarChar, 500, ParameterDirection.Input, couponImageUrl);
			AddCommandParameter(command, "@CouponDescription", SqlDbType.NVarChar, -1, ParameterDirection.Input, couponDescription);
			AddCommandParameter(command, "@IsActive", SqlDbType.Bit, 0, ParameterDirection.Input, isActive);
			AddCommandParameter(command, "@CreateDate", SqlDbType.DateTime, 0, ParameterDirection.Input, createDate);
			AddCommandParameter(command, "@ExpiryDate", SqlDbType.DateTime, 0, ParameterDirection.Input, expiryDate);
        }

        /// <summary>
        /// Binds the fetched data from data reader to Objects
        /// </summary>
        /// <param name="reader">Data reader supplied by BaseDAO</param>
        /// <returns>List of object instances that was bound</returns>
        protected override List<BaseDAO> BindData(DbDataReader reader)
        {
            List<BaseDAO> result = new List<BaseDAO>();

			if (reader == null)
                return null;

            while (reader.Read())
            {
                DiscountCoupon discountCoupon = new DiscountCoupon
                (
					reader["id"] == System.DBNull.Value ? GetIdMinValue : (int)reader["id"],
                    reader["coupon_name"] == System.DBNull.Value ? string.Empty : (string)reader["coupon_name"],
					reader["coupon_code"] == System.DBNull.Value ? string.Empty : (string)reader["coupon_code"],
					reader["coupon_discount_price"] == System.DBNull.Value ? 0.00 : (double)reader["coupon_discount_price"],
					reader["coupon_image_url"] == System.DBNull.Value ? string.Empty : (string)reader["coupon_image_url"],
					reader["coupon_description"] == System.DBNull.Value ? string.Empty : (string)reader["coupon_description"],
					reader["is_active"] == System.DBNull.Value ? false : (bool)reader["is_active"],
					reader["create_date"] == System.DBNull.Value ? SqlDateTime.MinValue.Value : (DateTime)reader["create_date"],
					reader["expiry_date"] == System.DBNull.Value ? SqlDateTime.MinValue.Value : (DateTime)reader["expiry_date"]
                );

                result.Add(discountCoupon);
            }
            
            if(!reader.IsClosed)
                reader.Close();

            return result;
        }

        /// <summary>
        /// List of store procedures that this class use
        /// </summary>
        private enum StoreProcedureName
        {
            Sp_DiscountCoupon_Save = 0,
            Sp_DiscountCoupon_Create = 1,
            Sp_DiscountCoupon_DeleteOne = 2,
            Sp_DiscountCoupon_DeleteMultiple = 3,
            Sp_DiscountCoupon_DeleteConditional = 4,
            Sp_DiscountCoupon_SaveConditional = 5,
            Sp_DiscountCoupon_LoadConditional = 6,
            Sp_DiscountCoupon_Load = 7,
            Sp_DiscountCoupon_LoadConditionalPaged = 8,
            Sp_DiscountCoupon_LoadConditionalPagedCount = 9,
            Sp_DiscountCoupon_LoadPaged = 10,
            Sp_DiscountCoupon_LoadPagedCount = 11
        }

        /// <summary>
        /// List of table column names that this class use
        /// </summary>
        private enum TableColumns
        {
            Id = 0,
			CouponName = 1,
			CouponCode = 2,
			CouponDiscountPrice = 3,
			CouponImageUrl = 4,
			CouponDescription = 5,
			IsActive = 6,
			CreateDate = 7,
			ExpiryDate = 8
        }
    }
}